package cate.game.res;

import cate.game.role.bag.IBagContainer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/** 分类了的ResList */
public class MixResClassified {

    /**
     * 是否有该类型的物品
     *
     * @param objectType 枚举来自GD.object.type.XXX
     */
    public boolean hasItem(byte objectType) {
        List<MixResItem> list = map.get(objectType);
        return list != null && !list.isEmpty();
    }

    private final Map<Byte, List<MixResItem>> map = new HashMap<>();

    private final Map<String, Integer> remainGridMap = new HashMap<>();

    public void recordBagGrid(String bagUid, int bagGrid) {
        remainGridMap.put(bagUid, bagGrid);
    }

    public int bagGrid(String bagUid) {
        return remainGridMap.getOrDefault(bagUid, 0);
    }

    /** @param objectType 枚举来自GD.object.type.XXX */
    public List<MixResItem> getItemList(byte objectType) {
        return map.computeIfAbsent(objectType, v -> new ArrayList<>());
    }

    public MixResClassified(MixRes resList) {
        parse(resList);
    }

    public void parse(MixRes resList) {
        if (resList == null || resList.items == null || resList.items.isEmpty()) {
            return;
        }
        resList.items.forEach(resItem -> getItemList(resItem.type).add(resItem));
    }
}
